package com.idroid.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * @author oushuhua
 */
public class KuCircleImageView extends ImageView {
    private Paint mPaint = new Paint();
    private static final int COLORDRAWABLE_DIMENSION = 1;
    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;

    public KuCircleImageView(Context context) {
        super(context);
    }

    public KuCircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KuCircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null != drawable) {
            Bitmap b = getRounderBitmap(getBitmapFromDrawable(drawable));
            final Rect rect = new Rect(0, 0, b.getWidth(), b.getHeight());
            mPaint.reset();
            canvas.drawBitmap(b, rect, rect, mPaint);

        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getRounderBitmap(Bitmap bitmap) {
        int wid = bitmap.getWidth();
        int hei = bitmap.getHeight();
        int radius = wid < hei ? wid / 2 : hei / 2;
        final Rect rect = new Rect(0, 0, wid, hei);
        Bitmap output = Bitmap.createBitmap(wid, hei, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        canvas.drawARGB(0, 0, 0, 0);
        mPaint.setColor(Color.BLUE);
        mPaint.setAntiAlias(true);

        canvas.drawCircle(wid / 2, hei / 2, radius, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, mPaint);

        return output;
    }

    private Bitmap getBitmapFromDrawable(Drawable drawable) {
        if (drawable == null) {
            return null;
        }

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        try {
            Bitmap bitmap;

            if (drawable instanceof ColorDrawable) {
                bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
            } else {
                bitmap = Bitmap.createBitmap(getWidth(), getHeight(), BITMAP_CONFIG);
            }

            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        } catch (OutOfMemoryError e) {
            return null;
        }
    }
}
